/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import core.Group;
import core.Student;
import db.dao.GroupDAO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mikael
 */
public class GroupPageBean {
    private Group group;
    private List<Student> members;

    private boolean owner;
    private boolean member;

    @PostConstruct
    public void init(){
        group = new Group();
        members = new ArrayList<Student>();
        owner = false;
    }

    public Group getGroup() {
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);

        ViewChangeBean vcb =
                (ViewChangeBean)https.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        Integer groupId;
        try{
            groupId = Integer.parseInt(vcb.getParam());
        }catch(Exception e){
            groupId = 1;
        }

        group = GroupDAO.read(groupId);

        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public List<Student> getMembers() {
        members.clear();

        members = GroupDAO.getStudents(group.getId());

        return members;
    }

    public void setMembers(List<Student> members) {
        this.members = members;
    }

    public boolean isMember() {
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);

        LoginBean lb = (LoginBean)https.getAttribute(LoginBean.MANGED_BEAN_NAME);

        ViewChangeBean vcb =
                (ViewChangeBean)https.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        Integer groupId;
        try{
            groupId = Integer.parseInt(vcb.getParam());
        }catch(Exception e){
            groupId = 1;
        }

        member = GroupDAO.isMember(groupId, lb.getId());

        return member;
    }

    public void setMember(boolean member) {
        this.member = member;
    }

    public boolean isOwner() {
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);

        LoginBean lb = (LoginBean)https.getAttribute(LoginBean.MANGED_BEAN_NAME);

        ViewChangeBean vcb =
                (ViewChangeBean)https.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        Integer groupId;
        try{
            groupId = Integer.parseInt(vcb.getParam());
        }catch(Exception e){
            groupId = 1;
        }

        owner = GroupDAO.isOwner(groupId, lb.getId());
        
        return owner;
    }

    public void setOwner(boolean owner) {
        this.owner = owner;
    }

    public boolean isMemberButNotOwner() {
        return isMember() && !isOwner();
    }

    public void editListener(ActionEvent e) {
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);
        ViewChangeBean vcb =
                (ViewChangeBean)https.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        vcb.setToView(e.getComponent().getId());

        GroupDAO.update(group);
    }

    public void deleteListener(ActionEvent e) {
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);
        ViewChangeBean vcb =
                (ViewChangeBean)https.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        vcb.setToView(e.getComponent().getId());
        
        GroupDAO.delete(group);
    }

    public void joinListener(ActionEvent e) {
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);
        ViewChangeBean vcb =
                (ViewChangeBean)https.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        vcb.setToView(e.getComponent().getId());
        
        LoginBean lb = (LoginBean)https.getAttribute(LoginBean.MANGED_BEAN_NAME);

        GroupDAO.join(group.getId(), lb.getId());
    }

    public void leaveListener(ActionEvent e) {
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);
        ViewChangeBean vcb =
                (ViewChangeBean)https.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        vcb.setToView(e.getComponent().getId());

        LoginBean lb = (LoginBean)https.getAttribute(LoginBean.MANGED_BEAN_NAME);

        GroupDAO.leave(group.getId(), lb.getId());
    }
}
